package com.camtc.camcenter.handler;

import com.camtc.camcenter.custom.CamHttpResult;
import com.camtc.camcenter.enums.CustomErrorInfo;
import com.camtc.camcenter.exception.CustomException;
import com.camtc.camcenter.util.ExceptionUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Priority;

/**
 * 异常处理
 *
 * @author
 * @date
 */

@ControllerAdvice
@Priority(1)
public class ExceptionAdviceHandler {

    /**
     * 捕捉自定义抛出异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public Object handleBizException(CustomException ex) {
        return CamHttpResult.failure(ex.getCode(), ex.getMessage());
    }

    /**
     * 系统异常捕获
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception ex) {
        ExceptionUtil.logExceptionInfo(ex);
        return CamHttpResult.failure(CustomErrorInfo.UNKNOW_ERROR);
    }
}
